var t = getApp(),
  e = t.requirejs("core");
Page({
  data: {
    route: "member_favorite",
    menu: t.tabBar,
    menu_show: false,
    icons: t.requirejs("icons"),
    page: 1,
    loading: false,
    isedit: false, //默认非编辑状态
    isCheckAll: false,//默认非全选状态
    checkNum: 0,//为0时删除无效
    loaded: false,
    list: [],
    delBtnWidth: 80//删除按钮宽度单位（rpx）
  },
  //底部导航跳转
  menu_url: function (k) {
    e.menu_url(k, 2);
  },
  onLoad: function (options) {

    if (options != null && options != undefined) {
      this.setData({
        tabbar_index: options.tabbar_index ? options.tabbar_index : -1
      });
    }

    e.setting();
    this.setData({
      menu: getApp().tabBar
    })

    if (this.data.tabbar_index >= 0) {
      this.setData({
        showtabbar: true,
      })
    }
    
      this.getList()
  },
 
   /**
   *上拉加载
   */
  onReachBottom: function () {
    this.data.loaded || this.getList()
  },

  /**
   *下拉刷新
   */
  onPullDownRefresh: function () {
    this.getList()
    wx.stopPullDownRefresh()
  },

  /**
   *收藏商品列表
   * @param goodsid
   * @param checkObj bool值
   * @return bool
   */
  getList: function () {
    var t = this;
    t.setData({
      loading: true
    }),
      e.get("user/GetFavorites", {
        page: t.data.page,
        uid: getApp().getCache("userinfo").uid
      }, function (e) {
        var a = {
          loading: false,
          show: true
        };
        e.info.length > 0 && (a.page = t.data.page + 1, a.list = t.data.list.concat(e.info), e.info.length <10 && (a.loaded = true)),//concat() 方法用于连接两个或多个数组。
          t.setData(a)
      })
  },
  //点击按钮
  btnClick: function(t){
    var a = this,
      i = e.pdata(t).action;
    
      if(i=='edit'){
        var s = {};
        for (var c in this.data.list) {
          s[this.data.list[c].log_id] = false
        }
        //console.log(s)
        a.setData({
          isedit: true,
          checkObj: s,//每个商品默认非选中状态
          isCheckAll: false //全选
        })
      } else if ("delete" == i) {
          var s = a.data.checkObj,
            l = [];
          for (var c in s)
            s[c] && l.push(c);//push() 方法可把它的参数顺序添加到 arrayObject 的尾部
          if (l.length < 1)
            return;
          l = l.toString()
          e.confirm("删除后不可恢复，确定要删除吗？", function () {
            e.get("goods/DelFavorites", {
              log_id: l
            }, function (t) {
                0 == t.code ? (
                  e.success('删除成功'),
                  a.setData({
                  isedit: false,
                  checkNum: 0,
                  page:1,
                  list:[]
                  }),
                  a.getList()
                  ): (e.alert(t.msg), setTimeout(function () {
                    wx.navigateBack()
                  }, 1e3))
             
              
            })
          })
      } else
        "finish" == i && a.setData({
          isedit: false,
          checkNum: 0
        })
  },

  //点击全选
  checkAllClick: function(t) {
    var t = !this.data.isCheckAll,
      e = this.data.checkObj,
      a = {
        isCheckAll: t,
        checkObj: e
      };
    for (var i in e)
      a.checkObj[i] =t;
    a.checkNum = t ? this.data.list.length : 0,
      this.setData(a)
  },

  //点击单选
  itemClick: function (t) {
    var a = this,
      i = e.pdata(t).id,
      s = e.pdata(t).goodsid;
    if (a.data.isedit) {//编辑时
      var c = a.data.checkObj,
        l = a.data.checkNum;
      c[i] ? (c[i] = false, l--) : (c[i] = true, l++);
      var o = true;
      for (var n in c)
        if (!c[n]) {
          o = false;
          break
        }//有一个未选中，则为非全选状态
      console.log(c)
      a.setData({
        checkObj: c,
        isCheckAll: o,
        checkNum: l
      })
    } else
      wx.navigateTo({
        url: "/yb_shop/pages/goods/detail/index?id=" + s
      })
  },
  /**
     * 滑动删除页面效果
  */
  touchS: function (e) {
    if (e.touches.length == 1) {
      this.setData({
        //设置触摸起始点水平方向位置
        startX: e.touches[0].clientX
      });
    }
  },
  touchM: function (e) {
    if (e.touches.length == 1) {
      //手指移动时水平方向位置
      var moveX = e.touches[0].clientX;
      //手指起始点位置与移动期间的差值
      var disX = this.data.startX - moveX;
      var delBtnWidth = this.data.delBtnWidth;
      var txtStyle = "";
      if (disX == 0 || disX < 0) {//如果移动距离小于等于0，文本层位置不变
        txtStyle = "left:0px";
      } else if (disX > 0) {//移动距离大于0，文本层left值等于手指移动距离
        txtStyle = "left:-" + disX + "px";
        if (disX >= delBtnWidth) {
          //控制手指移动距离最大值为删除按钮的宽度
          txtStyle = "left:-" + delBtnWidth + "px";
        }
      }
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        list: list
      });
    }
  },
  touchE: function (e) {
    console.log(e)
    if (e.changedTouches.length == 1) {
      //手指移动结束后水平位置
      var endX = e.changedTouches[0].clientX;
      //触摸开始与结束，手指移动的距离
      var disX = this.data.startX - endX;
      var delBtnWidth = this.data.delBtnWidth;
      //如果距离小于删除按钮的1/2，不显示删除按钮
      var txtStyle = disX > delBtnWidth / 2 ? "left:-" + delBtnWidth + "px" : "left:0px";
      //获取手指触摸的是哪一项
      var index = e.currentTarget.dataset.index;
      var list = this.data.list;
      list[index].txtStyle = txtStyle;
      //更新列表的状态
      this.setData({
        list: list
      });
    }
  },
  //获取元素自适应后的实际宽度
  getEleWidth: function (w) {
    var real = 0;
    try {
      var res = wx.getSystemInfoSync().windowWidth;
      var scale = (750 / 2) / (w / 2);//以宽度750px设计稿做宽度的自适应
      // console.log(scale);
      real = Math.floor(res / scale);
      return real;
    } catch (e) {
      return false;
      // Do something when catch error
    }
  },
  initEleWidth: function () {
    var delBtnWidth = this.getEleWidth(this.data.delBtnWidth);
    this.setData({
      delBtnWidth: delBtnWidth
    });
  },
  //点击删除按钮事件
  delItem: function (i) {
    var that = this,
      id = e.data(i).id; //获取列表中要删除项的id
    e.confirm("是否确认删除该商品?", function () {
      e.get("goods/DelFavorites", {
        log_id: id
      }, function (t) {
        0 == t.code ? (
          e.success('删除成功'),
          setTimeout(function () {
            that.setData({
              isedit: false,
              checkNum: 0,
              page: 1,
              list: []
            }),
              that.getList()
          }, 1e3)
          
        ) : (e.alert(t.msg))



      });
    })

  },
})